package com.zhongfl.crm.api.view;

import com.zhongfl.crm.lixiao.config.JobSwitchConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.heptagram.core.common.rest.ResponseResult;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author wang.yuchuan
 * @date 2018/9/29
 */
@RestController
@RequestMapping("/api/view/jobs")
@Slf4j
public class JobSwitchRestController {

    @Resource
    private JobSwitchConfigProperties jobSwitchConfigProperties;

    @GetMapping("/switch/set")
    public ResponseResult setSwitch(HttpServletRequest request) {
        String job = request.getParameter("job");
        if(StringUtils.isEmpty(job)){
            ResponseResult responseResult = new ResponseResult();
            responseResult.setRetcode(500);
            responseResult.setMsg("请设置job名称, 可选 department, user, product, channel, customerOrder, revisitLog");
            return responseResult;
        }
        String runnable = request.getParameter("runnable");

        if(StringUtils.isEmpty(runnable)){
            ResponseResult responseResult = new ResponseResult();
            responseResult.setRetcode(500);
            responseResult.setMsg("请设置runnable参数, true表示运行, false表示停止运行");
            return responseResult;
        }

        Boolean runnableResult = Boolean.valueOf(runnable);

        switch (job) {
            case "department":
                jobSwitchConfigProperties.getLx2boss().setDepartment(runnableResult);
                break;
            case "user":
                jobSwitchConfigProperties.getLx2boss().setUser(runnableResult);
                break;
            case "product":
                jobSwitchConfigProperties.getLx2boss().setProduct(runnableResult);
                break;
            case "channel":
                jobSwitchConfigProperties.getLx2boss().setChannel(runnableResult);
                break;
            case "customerOrder":
                jobSwitchConfigProperties.getLx2boss().setCustomerOrder(runnableResult);
                break;
            case "revisitLog":
                jobSwitchConfigProperties.getLx2boss().setRevisitLog(runnableResult);
                break;
            default:
                break;
        }
        return new ResponseResult();
    }

    @GetMapping("/switch/get")
    public ResponseResult<JobSwitchConfigProperties.Lx2BOSSJobSwitch> getSwitch() {

        return new ResponseResult<>(jobSwitchConfigProperties.getLx2boss());
    }
}
